package com.kylin.upms.biz.config.security;

import com.kylin.upms.biz.entity.Menu;
import com.kylin.upms.biz.entity.Role;
import com.kylin.upms.biz.service.IMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.CollectionUtils;
import org.springframework.util.SocketUtils;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 *
 */
@Component
@RefreshScope
public class KylinFilterInvocationSecurityMetadataSource implements FilterInvocationSecurityMetadataSource {
    @Autowired
    IMenuService iMenuService;
    @Value(value = "${urls}")
    private  String urls ;


    private final static List<String> add = Arrays.asList("/user/fileUpload");
    private final static List<String> role = Arrays.asList("/rolePage");
    private final static List<String> rolesele = Arrays.asList("/role/roleSele");
    private final static List<String> menu = Arrays.asList("/menu/getMenuAll1");
    private final static List<String> menubyid = Arrays.asList("/menu/getRoleByMenu");

    AntPathMatcher antPathMatcher = new AntPathMatcher();
    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    public Collection<ConfigAttribute> getAttributes(Object o) throws IllegalArgumentException {
        String requestUrl = ((FilterInvocation)o).getRequestUrl();


        if(urls.contains(requestUrl)){
            System.out.println("88889999"+urls);
            return SecurityConfig.createList("ROLE_LOGIN");
        }
        if(add.contains(requestUrl)){
            return SecurityConfig.createList("ROLE_LOGIN");
        }
        if(role.contains(requestUrl)){
            return SecurityConfig.createList("ROLE_LOGIN");
        }
        if(rolesele.contains(requestUrl)){
            return SecurityConfig.createList("ROLE_LOGIN");
        }
        if(menu.contains(requestUrl)){
            return SecurityConfig.createList("ROLE_LOGIN");
        }
        if(menubyid.contains(requestUrl)){
            return SecurityConfig.createList("ROLE_LOGIN");
        }


        logger.debug("请求的url为：{}",requestUrl);


        List<Menu> menuAll = iMenuService.getMenuAll1();
        for (Menu menu:menuAll){


            logger.debug("数据库中的url是:{}",menu.getUrl(),requestUrl);


            if(antPathMatcher.match(menu.getUrl(),requestUrl)&&!CollectionUtils.isEmpty(menu.getRoleList())){
                String[] roles = new String[menu.getRoleList().size()];
                int i = 0;
                for(Role role:menu.getRoleList()){
                    roles[i] = role.getName();
                    i++;
                }


                logger.debug("url:{}对应的角色信息:{}",requestUrl, Arrays.toString(roles));


                return SecurityConfig.createList(roles);
            }
        }


        logger.debug("请求的url:{}，没有匹配上角色信息，默认角色为:{}",requestUrl,"ROLE_NONE");


        return SecurityConfig.createList("ROLE_NONE");


    }

    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        return null;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return false;
    }
}
